package com.homepage.web.rest;

import com.homepage.repository.NewsImageRepository;
import com.homepage.service.NewsImageService;
import com.homepage.service.dto.NewsImageDTO;
import com.homepage.util.HeaderUtil;
import com.homepage.util.ResponseUtil;
import com.homepage.web.rest.errors.BadRequestAlertException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;

/**
 * REST controller for managing {@link com.homepage.domain.NewsImage}.
 */
@RestController
@RequestMapping("/company")
public class NewsImageResource {

    private final Logger log = LoggerFactory.getLogger(NewsImageResource.class);

    private static final String ENTITY_NAME = "newsImage";

    @Value("${common.clientApp.name}")
    private String applicationName;

    private final NewsImageService newsImageService;

    private final NewsImageRepository newsImageRepository;

    public NewsImageResource(NewsImageService newsImageService, NewsImageRepository newsImageRepository) {
        this.newsImageService = newsImageService;
        this.newsImageRepository = newsImageRepository;
    }

    /**
     * {@code POST  /news-images} : Create a new newsImage.
     *
     * @param newsImageDTO the newsImageDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new newsImageDTO, or with status {@code 400 (Bad Request)} if the newsImage has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/news-images")
    public ResponseEntity<NewsImageDTO> createNewsImage(@Valid @RequestBody NewsImageDTO newsImageDTO) throws URISyntaxException {
        log.debug("REST request to save NewsImage : {}", newsImageDTO);
        NewsImageDTO result = newsImageService.save(newsImageDTO);
        return ResponseEntity
            .created(new URI("/api/news-images/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /news-images/:id} : Updates an existing newsImage.
     *
     * @param id the id of the newsImageDTO to save.
     * @param newsImageDTO the newsImageDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated newsImageDTO,
     * or with status {@code 400 (Bad Request)} if the newsImageDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the newsImageDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/news-images/{id}")
    public ResponseEntity<NewsImageDTO> updateNewsImage(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody NewsImageDTO newsImageDTO
    ) throws URISyntaxException {
        newsImageDTO.setId(id);
        log.debug("REST request to update NewsImage : {}, {}", id, newsImageDTO);

        if (!newsImageRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        NewsImageDTO result = newsImageService.save(newsImageDTO);
        return ResponseEntity
            .ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, newsImageDTO.getId().toString()))
            .body(result);
    }


    /**
     * {@code GET  /news-images/:id} : get the "id" newsImage.
     *
     * @param id the id of the newsImageDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the newsImageDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/news-images/{id}")
    public ResponseEntity<NewsImageDTO> getNewsImage(@PathVariable Long id) {
        log.debug("REST request to get NewsImage : {}", id);
        Optional<NewsImageDTO> newsImageDTO = newsImageService.findOne(id);
        return ResponseUtil.wrapOrNotFound(newsImageDTO);
    }

    /**
     * {@code DELETE  /news-images/:id} : delete the "id" newsImage.
     *
     * @param id the id of the newsImageDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/news-images/{id}")
    public ResponseEntity<Void> deleteNewsImage(@PathVariable Long id) {
        log.debug("REST request to delete NewsImage : {}", id);
        newsImageService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
            .build();
    }
}
